package zmee.pur.src.common;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.scm.pds.common.util.PdsCommonUtils;
import kd.scm.src.common.calc.SrcCalcContext;
import kd.scm.src.common.calc.rank.ISrcRankResult;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: xw
 * @Date: 2023/01/05/15:22
 * @Description:
 */
public class SrcRankHandleLastRankExt implements ISrcRankResult {
    private static final long serialVersionUID = 1L;

    public SrcRankHandleLastRankExt() {
    }

    @Override
    public void process(SrcCalcContext context) {
        long billId = context.getBillId();
        QFilter filter = new QFilter("parentid", "=", billId);
        DynamicObject compareConfig = BusinessDataServiceHelper.loadSingle("src_compare_config", "extfilter", filter.toArray());
        DynamicObject extfilter = compareConfig.getDynamicObject("extfilter");
        if (extfilter != null) {
            String number = extfilter.getString("number");
            if ("negotiate004".equals(number)) {
                return;
            }
        }
        List<DynamicObject> assessList = context.getAllAssessList();
        if (null != assessList && assessList.size() != 0) {
            QFilter qfilter = new QFilter("billid", "=", context.getBillId());
            qfilter.and("istender", "=", "1");
            DynamicObject[] supplierRows = BusinessDataServiceHelper.load("src_bidopensupplier", "suppliertype,supplier,package," +
                    "currentrank,sumscore", qfilter.toArray());
            if (supplierRows.length != 0) {
                Map<String, List<DynamicObject>> supplierMap = new HashMap(2);
                Map<String, List<DynamicObject>> assessMap = new HashMap(2);
                String var7 = context.getSumType();
                byte var8 = -1;
                switch (var7.hashCode()) {
                    case 49:
                        if (var7.equals("1")) {
                            var8 = 0;
                        }
                        break;
                    case 50:
                        if (var7.equals("2")) {
                            var8 = 1;
                        }
                        break;
                    case 51:
                        if (var7.equals("3")) {
                            var8 = 2;
                        }
                }

                switch (var8) {
                    case 0:
                        supplierMap = (Map) Arrays.asList(supplierRows).stream().collect(Collectors.groupingBy((e) -> {
                            return e.getString("supplier.id");
                        }));
                        assessMap = (Map) assessList.stream().collect(Collectors.groupingBy((e) -> {
                            return e.getString("supplier_id");
                        }));
                        break;
                    case 1:
                        supplierMap = (Map) Arrays.asList(supplierRows).stream().collect(Collectors.groupingBy((e) -> {
                            return e.getString("package.id") + '_' + e.getString("supplier.id");
                        }));
                        assessMap = (Map) assessList.stream().collect(Collectors.groupingBy((e) -> {
                            return e.getString("package_id") + '_' + e.getString("supplier_id");
                        }));
                        break;
                    case 2:
                        supplierMap = (Map) Arrays.asList(supplierRows).stream().collect(Collectors.groupingBy((e) -> {
                            return e.getString("package.id") + '_' + e.getString("supplier.id");
                        }));
                        assessMap = (Map) assessList.stream().collect(Collectors.groupingBy((e) -> {
                            return e.getString("package_id") + '_' + e.getString("supplier_id");
                        }));
                }

                this.setTenderSupplierValue((Map) supplierMap, (Map) assessMap);
                PdsCommonUtils.saveDynamicObjects(supplierRows);
            }
        }
    }

    protected void setTenderSupplierValue(Map<String, List<DynamicObject>> supplierMap, Map<String, List<DynamicObject>> assessMap) {
        Iterator var3 = supplierMap.entrySet().iterator();

        while (var3.hasNext()) {
            Map.Entry<String, List<DynamicObject>> entry = (Map.Entry) var3.next();
            int currentRank = 0;
            BigDecimal sumScore = BigDecimal.ZERO;
            List<DynamicObject> assessList = (List) assessMap.get(entry.getKey());
            if (null != assessList && assessList.size() > 0) {
                List<DynamicObject> sortedList = this.rankByRank_Score(assessList);
                currentRank = ((DynamicObject) sortedList.get(0)).getInt("rank");
                sumScore = ((DynamicObject) sortedList.get(0)).getBigDecimal("sumscore");
            }

            Iterator var10 = ((List) entry.getValue()).iterator();

            while (var10.hasNext()) {
                DynamicObject obj = (DynamicObject) var10.next();
                obj.set("sumscore", sumScore);
                obj.set("currentrank", currentRank);
            }
        }

    }

    private List<DynamicObject> rankByRank_Score(List<DynamicObject> unSortedobjs) {
        Collections.sort(unSortedobjs, new Comparator<DynamicObject>() {
            @Override
            public int compare(DynamicObject e1, DynamicObject e2) {
                int rank1 = e1.getInt("rank");
                int rank2 = e2.getInt("rank");
                if (rank1 == rank2) {
                    BigDecimal score1 = e1.getBigDecimal("sumscore");
                    BigDecimal score2 = e2.getBigDecimal("sumscore");
                    return score2.compareTo(score1);
                } else {
                    return rank2 - rank1 < 0 ? 0 : -1;
                }
            }
        });
        return unSortedobjs;
    }
}